package com.wyj.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(value = false)// 懒加载进行关闭 只要已启动项目该类必定加载
public class ApplicationContestProvider implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContestProvider.applicationContext = applicationContext;
    }

    // 通过实例名字来获取到该实例  @Component("name") 根据name来进行获取
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    // 通过实例的Class对象来进行获取
    public static <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }


}



